package com.ccl.iot.huiju.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ccl.iot.IOTInterface;
import com.ccl.iot.IOTInterface.IOTIconMonitorInterface.IOTIconChangeListener;
import com.ccl.iot.R;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.device.IOTDeviceWithState;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.huiju.EditItemFragment;
import com.ccl.iot.huiju.IOTHuiJu;
import com.ccl.iot.huiju.db.IOTDaoUtil;
import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.views.MarqueeLabel;

import java.util.List;

/**
 * @description：
 * @author： HD
 * @time：2017/11/06
 */

public class EditAdapter extends BaseAdapter {


    private LayoutInflater mInflater;
    private List<IOTHuiJu> mDatas;
    public Context mContext;
    private int mEditPage;
    private AlertDialog mAlertDialog;

    public EditAdapter(Context context, List<IOTHuiJu> datas, int page) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
        mEditPage = page;
    }

    public void notifyDataSetChanged(List<IOTHuiJu> datas, int page) {
        mDatas = datas;
        mEditPage = page;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_edit, null);
            viewHolder.mItemLayout = (RelativeLayout) convertView.findViewById(R.id.layout_add_item);
            viewHolder.mItemImg = (ImageView) convertView.findViewById(R.id.item_iamge);
            viewHolder.mItemNameTv = (MarqueeLabel) convertView.findViewById(R.id.item_name);

            viewHolder.mSelectedLayout = (FrameLayout) convertView.findViewById(R.id.layout_select);

            viewHolder.mPositionIndexImg = (ImageView) convertView.findViewById(R.id.iv_index);
            viewHolder.mPageIndexImg = (ImageView) convertView.findViewById(R.id.iv_page);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mDatas != null) {
            int size = mDatas.size();
            if (position < size) {

                final IOTHuiJu iotHuiJu = mDatas.get(position);
                if (iotHuiJu != null) {

                    viewHolder.mItemNameTv.setTextColor(Color.WHITE);
                    viewHolder.mSelectedLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LogUtil.i(EditItemFragment.TAG, "onClick-----------");
                            if (mOnEditItemFragmentListener != null) {
                                mOnEditItemFragmentListener.onEditPositionSelected(iotHuiJu, mEditPage, position);
                            }
                        }
                    });

                    if (iotHuiJu instanceof IOTDevice) {
                        final IOTDevice device = (IOTDevice) iotHuiJu;

                        device.removeIconChangeListener(mIconChangeListener);
                        device.removeAuthChangeListener(mAuthChangeListener);
                        device.removeNameChangeListener(mNameChangeListener);
                        device.removeOnlineChangeListener(mOnlineChangeListener);

                        device.addIconChangeListener(mIconChangeListener);
                        device.addNameChangeListener(mNameChangeListener);
                        device.addAuthChangeListener(mAuthChangeListener);
                        device.addOnlineChangeListener(mOnlineChangeListener);


                            if (device instanceof IOTDeviceWithState) {
                                ((IOTDeviceWithState) device).removeStateChangeListener(mStateChangeListener);
                                ((IOTDeviceWithState) device).addStateChangeListener(mStateChangeListener);
                            }

                            int imageId = device.getImageId();
                            if (imageId != 0) {
                                //给设备设置对应的图片
                                viewHolder.mItemImg.setImageResource(imageId);
                            }

                            viewHolder.mItemNameTv.setText(device.getName());

                            viewHolder.mItemLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    device.onViewClick();
                                }
                            });


                            viewHolder.mItemLayout.setOnLongClickListener(new View.OnLongClickListener() {
                                @Override
                                public boolean onLongClick(View v) {

                                    if (mOnEditItemFragmentListener != null) {
                                        mOnEditItemFragmentListener.onEditRemoveIOTHuiJu(device);
                                    }

                                    return true;
                                }
                            });


                    } else if (iotHuiJu instanceof IOTBatch) {
                        final IOTBatch iotBatch = (IOTBatch) iotHuiJu;

                        int imgIndex = iotBatch.getImgIndex();
                        if (imgIndex < 0 || imgIndex >= Consts.BATCH_IMG_IDS.size()) {
                            imgIndex = 0;
                        }

                        viewHolder.mItemImg.setImageResource(Consts.BATCH_IMG_IDS.get(imgIndex));

                        viewHolder.mItemNameTv.setText(iotBatch.getName());

                        viewHolder.mItemLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                iotBatch.execute();
                            }
                        });

                        viewHolder.mItemLayout.setOnLongClickListener(new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {

                                LogUtil.e(EditItemFragment.TAG, "onLongClick2----");

                                if (mOnEditItemFragmentListener != null) {
                                    mOnEditItemFragmentListener.onEditRemoveIOTHuiJu(iotBatch);
                                }

                                return true;
                            }
                        });

                    }

                    setPagePositionImg(viewHolder, iotHuiJu);
                }
            }
        }


        return convertView;
    }


    private void setPagePositionImg(ViewHolder viewHolder, int pageIndex, int positionIndex) {

        int pageImageId = R.drawable.icon_page_1;
        int positionImageId = R.drawable.icon_index_0;


        switch (pageIndex) {
            case -1:
                pageImageId = R.drawable.icon_page_no;
                break;
            case 0:
                pageImageId = R.drawable.icon_page_1;
                break;
            case 1:
                pageImageId = R.drawable.icon_page_2;
                break;
            case 2:
                pageImageId = R.drawable.icon_page_3;
                break;
            case 3:
                pageImageId = R.drawable.icon_page_4;
                break;
            case 4:
                pageImageId = R.drawable.icon_page_5;
                break;
            case 5:
                pageImageId = R.drawable.icon_page_6;
                break;
            case 6:
                pageImageId = R.drawable.icon_page_7;
                break;
            case 7:
                pageImageId = R.drawable.icon_page_8;
                break;
            case 8:
                pageImageId = R.drawable.icon_page_9;
                break;
            default:
                break;
        }

        viewHolder.mPageIndexImg.setImageResource(pageImageId);

        switch (positionIndex) {
            case -1:
                positionImageId = R.drawable.icon_index_0;
                break;
            case 0:
                positionImageId = R.drawable.icon_index_1;
                break;
            case 1:
                positionImageId = R.drawable.icon_index_2;
                break;
            case 2:
                positionImageId = R.drawable.icon_index_3;
                break;
            case 3:
                positionImageId = R.drawable.icon_index_4;
                break;
            case 4:
                positionImageId = R.drawable.icon_index_5;
                break;
            case 5:
                positionImageId = R.drawable.icon_index_6;
                break;
            default:
                break;
        }

        viewHolder.mPositionIndexImg.setImageResource(positionImageId);
    }

    private void setPagePositionImg(ViewHolder viewHolder, IOTHuiJu iotHuiJu) {

        if (iotHuiJu instanceof IOTDevice) {
            final IOTDevice device = (IOTDevice) iotHuiJu;
            int pageIndex = device.getPageIndex();
            int positionIndex = device.getPositionIndex();

            setPagePositionImg(viewHolder, pageIndex, positionIndex);

        } else if (iotHuiJu instanceof IOTBatch) {
            final IOTBatch iotBatch = (IOTBatch) iotHuiJu;

            int pageIndex = iotBatch.getPageIndex();
            int positionIndex = iotBatch.getPositionIndex();

            setPagePositionImg(viewHolder, pageIndex, positionIndex);
        }

    }


    public void deleteIOTHuiJu(IOTHuiJu iotHuiJu) {

        if (iotHuiJu instanceof IOTDevice) {
            final IOTDevice device = (IOTDevice) iotHuiJu;
            int pageIndex = device.getPageIndex();
            int positionIndex = device.getPositionIndex();

            IOTDaoUtil.deletePositionData(pageIndex, positionIndex);

            device.setPageIndex(-1);
            device.setPositionIndex(-1);

            notifyDataSetChanged();

        } else if (iotHuiJu instanceof IOTBatch) {
            final IOTBatch iotBatch = (IOTBatch) iotHuiJu;

            int pageIndex = iotBatch.getPageIndex();
            int positionIndex = iotBatch.getPositionIndex();

            IOTDaoUtil.deletePositionData(pageIndex, positionIndex);

            iotBatch.setPageIndex(-1);
            iotBatch.setPositionIndex(-1);

            notifyDataSetChanged();

        }

    }


    private IOTInterface.IOTStateMonitorInterface.IOTStateChangeListener mStateChangeListener = new IOTInterface.IOTStateMonitorInterface.IOTStateChangeListener() {
        @Override
        public void onStateChanged(IOTInterface.IOTStateMonitorInterface aSource, int aNewState) {
            if (aSource != null && aSource instanceof IOTDevice) {
                LogUtil.i("6666","mStateChangeListener");
                IOTDevice dev = (IOTDevice) aSource;
                onDeviceChange(dev);
            }
        }
    };


    private IOTIconChangeListener mIconChangeListener = new IOTIconChangeListener() {
        @Override
        public void onIconChanged(IOTDevice dev, String icon) {
            if (dev != null) {
                LogUtil.i("6666","mIconChangeListener");
                onDeviceChange(dev);
            }
        }
    };

    private IOTInterface.IOTAuthMonitorInterface.IOTAuthChangeListener mAuthChangeListener = new IOTInterface.IOTAuthMonitorInterface.IOTAuthChangeListener() {
        @Override
        public void onAuthChanged(IOTDevice dev, int aNewAuth) {
            if (dev != null) {
                LogUtil.i("6666","mAuthChangeListener");
                onDeviceChange(dev);
            }
        }
    };


    private IOTInterface.IOTNameMonitorInterface.IOTNameChangeListener mNameChangeListener = new IOTInterface.IOTNameMonitorInterface.IOTNameChangeListener() {
        @Override
        public void onNameChanged(IOTDevice aSource, String aNewName) {
            if (aSource != null && aSource instanceof IOTDevice) {
                LogUtil.i("6666","mNameChangeListener");
                IOTDevice dev = (IOTDevice) aSource;
                onDeviceChange(dev);
            }
        }
    };


    private IOTInterface.IOTOnlineMonitorInterface.IOTOnlineStatusChangeListener mOnlineChangeListener = new IOTInterface.IOTOnlineMonitorInterface.IOTOnlineStatusChangeListener() {
        @Override
        public void onOnlineStatusChanged(IOTDevice dev, boolean aOnline) {
            if (dev != null) {
                LogUtil.i("6666","mOnlineChangeListener");
                onDeviceChange(dev);
            }
        }
    };

    private void onDeviceChange(IOTDevice dev) {
        UIUtils.postTaskSafely(notifyRunnable);
    }

    public Runnable notifyRunnable = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };

    public EditItemFragment.OnEditItemFragmentListener mOnEditItemFragmentListener;

    public void setOnEditItemFragmentListener(EditItemFragment.OnEditItemFragmentListener listener) {
        mOnEditItemFragmentListener = listener;
    }


    class ViewHolder {
        RelativeLayout mItemLayout;
        ImageView mItemImg;
        MarqueeLabel mItemNameTv;
        FrameLayout mSelectedLayout;
        ImageView mPositionIndexImg;
        ImageView mPageIndexImg;
    }
}